package com.kingint.web.listener.roomClean;

import com.kingint.mbg.ser.entity.SerWorkOrder;
import com.kingint.mbg.ser.mapper.SerWorkOrderMapper;
import com.kingint.web.util.spring.SpringContextHolder;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.util.StringUtils;

/**
 * @author N_Xiang
 * @describe 接受清洁工单监听类
 * @time 2021-01-10 下午 3:41
 */
public class roomCleanAcceptListener implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        SerWorkOrderMapper orderMapper = SpringContextHolder.getBean(SerWorkOrderMapper.class);
        String processInstanceId = delegateTask.getProcessInstanceId();//流程实例id
        String eventName = delegateTask.getEventName();//获取事件名称
        RuntimeService runtimeService = SpringContextHolder.getBean(RuntimeService.class);
        ProcessInstance instance = runtimeService.createProcessInstanceQuery()
                .processInstanceId(processInstanceId)
                .singleResult();
        if (eventName.equals("create")) {//创建时调用
            if(!StringUtils.isEmpty(instance)){
                String businessKey = instance.getBusinessKey();
                String[] s = businessKey.split("_");
                Integer integer = Integer.valueOf(s[2]);//workId
                SerWorkOrder workOrder = new SerWorkOrder();
                workOrder.setId(integer);
                workOrder.setOrderStatus(2);//已接单
                orderMapper.updateById(workOrder);//更新相关额状态
            }
        }
    }
}
